/**
 * Created by wzh on 2016/8/16.
 */
swan_pc.controller("UserProfileController",function($scope,$rootScope,UserProfile,Upload){
    $scope.profileStruct=[
        {
            name: "基本信息",
            type: "normal",
            blockName: "baseInfo",
            preName: "邮箱",
            struct: [
                {
                    name: "姓名",
                    key: "name",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                    required: true,
                },
                {
                    name: "别名",
                    key: "nickName",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                },
                {
                    name: "英文名",
                    key: "englishName",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                },
                {
                    name: "性别",
                    key: "gender",
                    option: 3,
                    select: [
                        {
                            name: "男",
                            value: 1
                        },
                        {
                            name: "女",
                            value: 0
                        }
                    ],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                },
                {
                    name: "公司",
                    key: "company",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                },
                {
                    name: "地位",
                    key: "position",
                    option: 1,
                    select: [],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                },
            ]
        },
        {
            name: "邮件列表",
            type: "repeat",
            blockName: "emailList",
            preName: "邮箱",
            struct: [
                {
                    name: "邮箱类型",
                    key: "name",
                    option: 3,
                    select: [
                        {
                            name: "默认",
                            value: "默认"
                        },
                        {
                            name: "专用",
                            value: "专用"
                        },
                        {
                            name: "家庭",
                            value: "家庭"
                        },
                        {
                            name: "办公",
                            value: "办公"
                        },
                        {
                            name: "私人",
                            value: "私人"
                        },
                    ],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: ":",
                    noneName: true,
                    rowItemNum: 5,
                },
                {
                    name: "邮箱",
                    key: "value",
                    option: 1,
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                    noneName: true,
                    rowItemNum: 5,
                    itemsPerRow: "four",
                    class: "margin-left-p3"
                }
            ]
        },
        {
            name: "地址列表",
            type: "repeat",
            blockName: "address",
            preName: "地址",
            struct: [
            {
                name: "三级地址菜单",
                key: ["province", "city", "district"],
                option: 4,
                select: [],
                show: [],
                preview: 5,
                preBefore: "",
                preAfter: "",
            },
            {
                name: "地区",
                key: "area",
                option: 1,
                select: [],
                show: [],
                preview: 2,
                preBefore: "",
                preAfter: "",
            },
            {
                name: "街道",
                key: "street",
                option: 1,
                select: [],
                show: [],
                preview: 2,
                preBefore: "",
                preAfter: "",
            },
            {
                name: "门牌号",
                key: "house",
                option: 1,
                select: [],
                show: [],
                preview: 3,
                preBefore: "",
                preAfter: "号",
            },
            {
                name: "详细地址",
                key: "detailAddress",
                option: 1,
                select: [],
                show: [],
                preview: 1,
                preBefore: ":",
                preAfter: "",
            },
            {
                name: "邮编",
                key: "postcode",
                option: 1,
                select: [],
                show: [],
                preview: 4,
                preBefore: "(",
                preAfter: ")"
            }
        ]
        },
        {
            name: "电话列表",
            type: "repeat",
            blockName: "numberList",
            preName: "电话",
            struct: [
                {
                    name: "电话类型",
                    key: "name",
                    option: 3,
                    select: [
                        {
                            name: "默认",
                            value: "默认"
                        },
                        {
                            name: "专用",
                            value: "专用"
                        },
                        {
                            name: "家庭",
                            value: "家庭"
                        },
                        {
                            name: "办公",
                            value: "办公"
                        },
                        {
                            name: "私人",
                            value: "私人"
                        },
                    ],
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: ":",
                    noneName: true,
                    rowItemNum: 5,
                },
                {
                    name: "号码",
                    key: "value",
                    option: 1,
                    show: [],
                    preview: 0,
                    preBefore: "",
                    preAfter: "",
                    noneName: true,
                    rowItemNum: 5,
                    itemsPerRow: "four",
                    class: "margin-left-p3"
                }
            ]
        },
    ]
    $scope.reset=function(){
        UserProfile.queryMe(function(result){
            $scope.presentProfile=result;
            $scope.profilePointer={
                baseInfo:result,
                emailList:result.emailList,
                address:result.addresses,
                numberList:result.phoneNumberList,
                photoList:result.pictures
            }
        },function(reason){
            if (reason.data.description=="您尚未创建个人名片"||reason.data.description=="您尚未创建个人名片，请创建"){
                $scope.presentProfile={
                    "addresses": [
                    ],
                    "company": "",
                    "emailList": [
                    ],
                    "englishName": "",
                    "gender": null,
                    "name": "",
                    "nickName": "",
                    "phoneNumberList": [
                    ],
                    "photoUrl": "",
                    "pictures": [
                    ],
                    "position": "",
                }
                $scope.profilePointer={
                    baseInfo:$scope.presentProfile,
                    emailList:$scope.presentProfile.emailList,
                    address:$scope.presentProfile.addresses,
                    numberList:$scope.presentProfile.phoneNumberList,
                    photoList:$scope.presentProfile.pictures
                }
            }
        })
    }
    $scope.reset();
    $scope.addElement=function(array){
        array.push({});
    }
    $scope.updateProfile=function(){
        _.forEach($scope.presentProfile.photoList,function(item){
            item.type="OTHER";
        })
        UserProfile.update($scope.presentProfile,function(){
            alert("保存成功")
        })
    }
    //照片上传部分:
    $scope.submit = function(item,yes) {
        if (item.$$photo!=undefined) {
            $scope.upload(item.$$photo,item,yes);
        }
    };
    $scope.upload = function (file,item,yes) {
        Upload.upload({
            url: '/api/uploadFile',
            data: {file: file}
        }).then(function (resp) {
            var location="http://www.diamondtiming.com:8686";
            if (yes) item.photoUrl=location+resp.data.path;
            else item.path=location+resp.data.path;
            item.$$photo=undefined;
        }, function (resp) {
            console.log('Error status: ' + resp.status);
        }, function (evt) {
            var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
            console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);
        });
    };
    $scope.deletePhoto=function(item){
        _.pull($scope.presentProfile.pictures,item)
    }
})